package com.thread.exe;

import java.util.Set;
import java.util.concurrent.Semaphore;

public class BoundedHashSet<T> {
	private final Set<T> set;
	private final Semaphore sem;
	
	public BoundedHashSet(Set<T> set, Semaphore sem) {
		super();
		this.set = set;
		this.sem = sem;
	}
	
	public boolean add(T o) throws InterruptedException {
		sem.acquire();
		boolean flag = false;
		try {
			flag = set.add(o);
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if(!flag)
				sem.release();
		}
		return flag;
	}
	
	public boolean remove(Object o) throws InterruptedException {
		boolean flag = set.remove(o);
		if(flag)
			sem.release();
		return flag;
	}
}
